package com.erpanderp.wam.dbmigration.model.service;

import java.util.List;

import org.hibernate.FlushMode;
import org.hibernate.Query;
import org.hibernate.Session;

import com.erpanderp.wam.ac.model.entity.reimbursement.BoardingExpense;
import com.erpanderp.wam.ac.model.entity.reimbursement.LodgingExpense;
import com.erpanderp.wam.ac.model.entity.reimbursement.TravelExpense;
import com.erpanderp.wam.ac.model.entity.reimbursement.TravelMiscExpense;
import com.erpanderp.wam.ac.model.entity.reimbursement.TransportExpense;
import com.erpanderp.wam.ac.model.entity.travel.TravelRequest;
import com.erpanderp.wam.dbmigration.model.entity.FlexDomesticLogistics;
import com.erpanderp.wam.dbmigration.model.entity.FlexEmployeeLeave;
import com.erpanderp.wam.dbmigration.model.entity.FlexExpenseTable;
import com.erpanderp.wam.dbmigration.model.entity.FlexInternationalLogistics;
import com.erpanderp.wam.dbmigration.model.entity.FlexLodgingExpense;
import com.erpanderp.wam.dbmigration.model.entity.FlexTravelMaster;
import com.erpanderp.wam.dbmigration.model.entity.FlexTravelMiscExpense;
import com.erpanderp.wam.dbmigration.model.entity.FlexTravelMiscExpenseBoarding;
import com.erpanderp.wam.dbmigration.model.entity.FlexExpenseTransportation;
import com.erpanderp.wam.usm.model.entity.leave.EmployeeLeave;
import com.erpanderp.wam.util.HibernateUtil;

public class TravelMasterDAO {

	public List<FlexExpenseTransportation> getFlexExpenseTransportsList() {
		List<FlexExpenseTransportation> flexExpenseTransportationList = null;
		System.out.println("IMPORTING RESOURCE EMPLOYEE MASTERS");
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.setFlushMode(FlushMode.AUTO);
			session.beginTransaction();
			String queryString = "from FlexExpenseTransportation fet";
			Query query = session.createQuery(queryString);
			flexExpenseTransportationList = (List<FlexExpenseTransportation>) query
					.list();

			System.out.println("FLEX FLEX EXPENSE TRANSPORTATION LIST SIZE IS "
					+ flexExpenseTransportationList.size());
			return flexExpenseTransportationList;

		} catch (Exception e) {
			e.printStackTrace();
			return null;
			// TODO: handle exception
		}

	}

	public List<FlexTravelMiscExpense> getFlexTravelMiscExpenses() {
		List<FlexTravelMiscExpense> flexTravelExpensesList = null;
		System.out.println("IMPORTING FlexTravelMiscExpense");
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.setFlushMode(FlushMode.AUTO);
			session.beginTransaction();
			String queryString = "from FlexTravelMiscExpense fme";
			Query query = session.createQuery(queryString);
			flexTravelExpensesList = (List<FlexTravelMiscExpense>) query.list();

			System.out.println("FLEX FLEX TRAVEL REQUESTS LIST SIZE IS "
					+ flexTravelExpensesList.size());
			return flexTravelExpensesList;

		} catch (Exception e) {
			e.printStackTrace();
			return null;
			// TODO: handle exception
		}

	}

	public List<FlexTravelMiscExpenseBoarding> getFlexTravelMiscExpensesBoardings() {
		List<FlexTravelMiscExpenseBoarding> flexTravelMiscExpensesBoardingList = null;
		System.out.println("IMPORTING RESOURCE EMPLOYEE MASTERS");
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.setFlushMode(FlushMode.AUTO);
			session.beginTransaction();
			String queryString = "from FlexTravelMiscExpenseBoarding ftmeb";
			Query query = session.createQuery(queryString);
			flexTravelMiscExpensesBoardingList = (List<FlexTravelMiscExpenseBoarding>) query
					.list();

			System.out.println("FLEX FLEX BOARDING LIST SIZE IS "
					+ flexTravelMiscExpensesBoardingList.size());
			return flexTravelMiscExpensesBoardingList;

		} catch (Exception e) {
			e.printStackTrace();
			return null;
			// TODO: handle exception
		}

	}

	public List<FlexLodgingExpense> getFlexLodigngExpenses() {
		List<FlexLodgingExpense> flexLodgingExpensesList = null;
		System.out.println("IMPORTING RESOURCE EMPLOYEE MASTERS");
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.setFlushMode(FlushMode.AUTO);
			session.beginTransaction();
			String queryString = "from FlexLodgingExpense fle";
			Query query = session.createQuery(queryString);
			flexLodgingExpensesList = (List<FlexLodgingExpense>) query.list();

			System.out.println("FLEX FLEX TRAVEL REQUESTS LIST SIZE IS "
					+ flexLodgingExpensesList.size());
			return flexLodgingExpensesList;

		} catch (Exception e) {
			e.printStackTrace();
			return null;
			// TODO: handle exception
		}

	}

	public List<FlexTravelMaster> getFlexTravelMaster() {
		List<FlexTravelMaster> flexTravelMasterList = null;
		System.out.println("IMPORTING RESOURCE EMPLOYEE MASTERS");
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.setFlushMode(FlushMode.AUTO);
			session.beginTransaction();
			String queryString = "from FlexTravelMaster f";
			Query query = session.createQuery(queryString);
			flexTravelMasterList = (List<FlexTravelMaster>) query.list();

			System.out.println("FLEX FLEX TRAVEL REQUESTS LIST SIZE IS "
					+ flexTravelMasterList.size());
			return flexTravelMasterList;

		} catch (Exception e) {
			e.printStackTrace();
			return null;
			// TODO: handle exception
		}

	}

	public List<FlexDomesticLogistics> getDomesticLogistics() {
		List<FlexDomesticLogistics> flexDomesticLogistics;
		System.out.println("IMPORTING RESOURCE EMPLOYEE MASTERS");
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.setFlushMode(FlushMode.AUTO);
			session.beginTransaction();
			String queryString = "from FlexDomesticLogistics f";
			Query query = session.createQuery(queryString);
			flexDomesticLogistics = (List<FlexDomesticLogistics>) query.list();
			session.getTransaction().commit();
			return flexDomesticLogistics;

		} catch (Exception e) {
			e.printStackTrace();
			return null;
			// TODO: handle exception
		}

	}

	public List<FlexInternationalLogistics> getInternationalLogistics() {
		List<FlexInternationalLogistics> logistics;
		System.out.println("IMPORTING RESOURCE EMPLOYEE MASTERS");
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.setFlushMode(FlushMode.AUTO);
			session.beginTransaction();
			String queryString = "from FlexInternationalLogistics f";
			Query query = session.createQuery(queryString);
			logistics = (List<FlexInternationalLogistics>) query.list();
			session.getTransaction().commit();
			return logistics;

		} catch (Exception e) {
			e.printStackTrace();
			return null;
			// TODO: handle exception
		}

	}

	public TravelRequest getTraveRequestByTravelRequestId(
			String travelRequestCode) {
		TravelRequest travelRequest = null;
		System.out.println("IMPORTING FLEX EXPENSES");
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.setFlushMode(FlushMode.AUTO);
			session.beginTransaction();
			String queryString = "from TravelRequest tr where tr.travelRequestCode=:travelRequestCode";
			Query query = session.createQuery(queryString).setString(
					"travelRequestCode", travelRequestCode);
			List temp = (List) query.list();
			if (temp != null && temp.size() > 0) {
				travelRequest = (TravelRequest) temp.get(0);
			}
			return travelRequest;

		} catch (Exception e) {
			e.printStackTrace();
			return null;
			// TODO: handle exception
		}
	}

	public TravelExpense getTravelExpenseIByTravelExpenseCode(
			String travelExpenseCode) {
		TravelExpense travelExpense = null;
		System.out.println("IMPORTING FLEX EXPENSES");
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.setFlushMode(FlushMode.AUTO);
			session.beginTransaction();
			String queryString = "from TravelExpense te where te.travelExpenseCode=:travelExpenseCode";
			Query query = session.createQuery(queryString).setCacheable(true)
					.setString("travelExpenseCode", travelExpenseCode);
			List temp = (List) query.list();
			if (temp != null && temp.size() > 0) {
				travelExpense = (TravelExpense) temp.get(0);
			}
			return travelExpense;

		} catch (Exception e) {
			e.printStackTrace();
			return null;
			// TODO: handle exception
		}
	}

	public List<FlexExpenseTable> getFlexExpenses() {
		List<FlexExpenseTable> flexExpensesList = null;
		System.out.println("IMPORTING FLEX EXPENSES");
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.setFlushMode(FlushMode.AUTO);
			session.beginTransaction();
			String queryString = "from FlexExpenseTable f";
			Query query = session.createQuery(queryString);
			flexExpensesList = (List<FlexExpenseTable>) query.list();

			System.out.println("FLEX FLEX TRAVEL REQUESTS LIST SIZE IS "
					+ flexExpensesList.size());
			return flexExpensesList;

		} catch (Exception e) {
			e.printStackTrace();
			return null;
			// TODO: handle exception
		}

	}

	public void createWamTravelRequests(List<TravelRequest> travelRequestList) {
		System.out.println("IN WAM EMPLOYEE LEAVES DATA EXPORTING");
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.setFlushMode(FlushMode.AUTO);
			session.beginTransaction();
			for (TravelRequest travelRequest : travelRequestList) {
				session.save(travelRequest);
			}
			session.getTransaction().commit();

		} catch (Exception e) {
			e.printStackTrace();

			// TODO: handle exception
		}
	}

	public void createWAMTravelExpenses(List<TravelExpense> travelExpensesList) {
		System.out.println("IMPORTING INTO WAM TRAVEL EXPENSE");
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.setFlushMode(FlushMode.AUTO);
			session.beginTransaction();
			for (TravelExpense travelExpense : travelExpensesList) {
				session.save(travelExpense);
			}
			session.getTransaction().commit();

		} catch (Exception e) {
			e.printStackTrace();

			// TODO: handle exception
		}
	}

	public void createWAMLodgingExpenses(
			List<LodgingExpense> wamLodgingExpenseList) {
		System.out.println("IMPORTING INTO WAM TRAVEL EXPENSE");
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.setFlushMode(FlushMode.AUTO);
			session.beginTransaction();
			for (LodgingExpense lodgingExpense : wamLodgingExpenseList) {
				session.save(lodgingExpense);
			}
			session.getTransaction().commit();

		} catch (Exception e) {
			e.printStackTrace();

			// TODO: handle exception
		}
	}

	public void createWAMTravelMiscExpenses(
			List<TravelMiscExpense> wamTravelMiscExpensesList) {
		System.out.println("IMPORTING INTO WAM TRAVEL EXPENSE");
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.setFlushMode(FlushMode.AUTO);
			session.beginTransaction();
			for (TravelMiscExpense travelMiscExpense : wamTravelMiscExpensesList) {
				session.save(travelMiscExpense);
			}
			session.getTransaction().commit();

		} catch (Exception e) {
			e.printStackTrace();

			// TODO: handle exception
		}
	}

	public void createWAMTravelBoardingMiscExpenses(
			List<BoardingExpense> wamBoardingExpensesList) {
		System.out.println("IMPORTING INTO WAM TRAVEL EXPENSE");
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.setFlushMode(FlushMode.AUTO);
			session.beginTransaction();
			for (BoardingExpense boardingExpense : wamBoardingExpensesList) {
				session.save(boardingExpense);
			}
			session.getTransaction().commit();

		} catch (Exception e) {
			e.printStackTrace();

			// TODO: handle exception
		}
	}

	public void createWAMExpenseTransport(
			List<TransportExpense> wamTransportExpenseList) {
		System.out.println("IMPORTING INTO WAM TRAVEL EXPENSE");
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.setFlushMode(FlushMode.AUTO);
			session.beginTransaction();
			for (TransportExpense transportExpense : wamTransportExpenseList) {
				session.save(transportExpense);
			}
			session.getTransaction().commit();

		} catch (Exception e) {
			e.printStackTrace();

			// TODO: handle exception
		}
	}

	public static void main(String q[]){
		List<FlexInternationalLogistics> temp = new TravelMasterDAO().getInternationalLogistics();
		System.out.println(temp);
		for(FlexInternationalLogistics domesticLogistics : temp){
			System.out.println(domesticLogistics.getTravel_Id() + " " + domesticLogistics.getEmpId() + " " + domesticLogistics.getLogistic());
		}
	}
}
